/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conexion;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Duoc
 */
public class BD {
    
    private static BD bd = null;
    private static String stServer = "localhost";
    private static String stPort = "3306";
    private static String stUser = "root";
    private static String stPass = "";
    private static String stBase = "ticketmarket";
    private static Statement stmt;
    
    private ResultSet rs;
    private Connection cnn;

    private BD() {
        try {
            this.conectar();
        } catch (Exception e) {
        }
    }

    private void conectar() throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            cnn = DriverManager.getConnection("jdbc:mysql://" + stServer + ":" + stPort + "/" + stBase, stUser, stPass);
            stmt = cnn.createStatement();
        } catch (ClassNotFoundException e) {
           
        }
    }

    public synchronized static BD getInstance() {
        if (bd == null) {
            bd = new BD();
        }
        return bd;
    }

    public boolean sqlEjecutar(String sql) {
        try {
            int resultado = stmt.executeUpdate(sql);

            return (resultado > 0);
        } catch (Exception e) {
            
        }
        return false;
    }
    
    public boolean sqlQuery(String sql)
    {
      
        try {
            rs = stmt.executeQuery(sql);
            return true;
            
        } catch (Exception e) {
        }
        return false;
    }
    
    
    
    //Obtener
    public boolean sqlFetch()
    {
        try 
        {
            return rs.next();
        } 
        catch (Exception ex) 
        {
            
            return false;
        }
    }
    public String getString(String campo)
    {
        try {
            return rs.getString(campo);
        } catch (Exception e) {
        }
        return null;
    }
     public int getInt(String campo)
    {
        try {
            return rs.getInt(campo);
        } catch (Exception e) {
        }
        return 0;
    }
     public char getChar(String campo)
    {
        try {
            return rs.getString(campo).charAt(0);
        } catch (Exception e) {
        }
        return '0';
    }
}
